package com.dropgame.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.dropgame.main.AssetLoader;
import com.dropgame.main.DropGame;
import com.dropgame.tools.ImageButton;
import com.dropgame.tools.TransitionEffect;

public class StoryScreen implements Screen{

	private DropGame game;
	private OrthographicCamera guiCam;
	
	private static final int VIRTUAL_WIDTH = 320;
    private static final int VIRTUAL_HEIGHT = 480;
    private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
    
    public Rectangle viewport;
	private TransitionEffect transitionEffect;
	private Input input;
	private SpriteBatch batcher;
	
	private Sprite gameBackGround;
	
	private Sprite level1Lock;
	private Sprite level2Lock;
	
	private ImageButton level1;
	private ImageButton level2;
	private ImageButton level3;
	private ImageButton level4;
	private ImageButton level5;
	private ImageButton level6;
	private ImageButton backButtonSmall;
	
	private boolean level1Playable = true;
	private boolean level2Playable = true;
	
	public StoryScreen(DropGame game){
		this.game = game;
        this.guiCam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);		
		guiCam.position.set(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2, 0);
		
		
		this.viewport = new Rectangle(0.0f,0.0f,0.0f,0.0f);
		this.transitionEffect = new TransitionEffect(game);
		this.input = Gdx.input;
		this.batcher = new SpriteBatch();
		
		
		gameBackGround = new Sprite(AssetLoader.gameBackgroundRegion);
		level1Lock = AssetLoader.level1Lock;
		level2Lock = AssetLoader.level2Lock;
		
		level1Lock.setPosition(10.0f, 335.0f);
		level2Lock.setPosition(95.0f, 335.0f);
		
		Sprite[] button1Array = {AssetLoader.level1Button};
		Sprite[] button2Array = {AssetLoader.level2Button};
		Sprite[] button3Array = {AssetLoader.level3Button};
		Sprite[] button4Array = {AssetLoader.level4Button};
		Sprite[] button5Array = {AssetLoader.level5Button};
		Sprite[] button6Array = {AssetLoader.level6Button};
		Sprite[] backArray = {AssetLoader.backButtonSmall};
		
		
		this.level1 = new ImageButton(0.15f, button1Array, input, guiCam, viewport, 10.0f, 335.0f);
		this.level2 = new ImageButton(0.15f, button2Array, input, guiCam, viewport, 95.0f, 335.0f);
		this.level3 = new ImageButton(0.15f, button3Array, input, guiCam, viewport, 175.0f, 335.0f);
		this.level4 = new ImageButton(0.15f, button4Array, input, guiCam, viewport, 10.0f, 250.0f);
		this.level5 = new ImageButton(0.15f, button5Array, input, guiCam, viewport, 95.0f, 250.0f);
		this.level6 = new ImageButton(0.15f, button6Array, input, guiCam, viewport, 175.0f, 250.0f);
		
		this.backButtonSmall = new ImageButton(0.15f, backArray, input, guiCam, viewport, 10.0f, 10.0f);
		
		
	}
	@Override
	public void render(float deltaTime) {
		GL10 gl = Gdx.app.getGraphics().getGL10();
		guiCam.update();
		guiCam.apply(gl);
		
		gl.glViewport((int) viewport.x, (int) viewport.y,
          	  		  (int) viewport.width, (int) viewport.height);
		
		gl.glClearColor(0, 0, 0, 0);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		batcher.setProjectionMatrix(guiCam.combined);
		batcher.begin();
		gameBackGround.draw(batcher);
		level1.getKeyFrame(deltaTime).draw(batcher);
		level2.getKeyFrame(deltaTime).draw(batcher);
		level3.getKeyFrame(deltaTime).draw(batcher);
		level4.getKeyFrame(deltaTime).draw(batcher);
		level5.getKeyFrame(deltaTime).draw(batcher);
		level6.getKeyFrame(deltaTime).draw(batcher);
		if(! game.multipleProfile.returnDefault().levels[0].playable) level1Lock.draw(batcher);
		if(! game.multipleProfile.returnDefault().levels[1].playable) level2Lock.draw(batcher);
		backButtonSmall.getKeyFrame(deltaTime).draw(batcher);
		batcher.end();
		
		transitionEffect.movingScreen(batcher, deltaTime);
		
		updateLogic(deltaTime);
		
	}
	
	public void updateLogic(float deltaTime){
		
		if(backButtonSmall.getCompleted()){
			transitionEffect.nextScreen(new FreeOrStoryScreen(game));
		}
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	

	@Override
	public void resize(int width, int height) {
		float aspectRatio = (float)width/(float)height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);
        if(aspectRatio > ASPECT_RATIO)
        {
            scale = (float)height/(float)VIRTUAL_HEIGHT;
            crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
        }
        else if(aspectRatio < ASPECT_RATIO)
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
            crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
        }
        else
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
        }

        float w = (float)VIRTUAL_WIDTH*scale;
        float h = (float)VIRTUAL_HEIGHT*scale;
        
        // Improved viewport resize
         //viewport = new Rectangle(crop.x, crop.y, w, h);
        viewport.set(crop.x, crop.y, w, h);
        
        //cameraSpriteButton.viewport = viewport;
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

}
